﻿@{
    ViewBag.Title = "Users";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@model e.Store.Model.Entities.User



<div class="col-lg-12">

    @using (Html.BeginForm("AddAndUpdateUser", "Administrator", FormMethod.Post, new { id = "userForm" }))
    {
        <div id="productCrud" class="panel panel-danger">
            <div class="panel-heading">
                <h3 class="panel-title"><span id="title">User</span> <span class="pull-right"><a href="/Administrator/Users" title="View All Users" class="btn-sm btn-primary">
                    <span class="btn-label"><i class="icon-table"></i></span>View All Users </a>
                </span></h3>
            </div>
            <div class="panel-body">

                <div class="col-lg-4 col-lg-offset-1">

                    @Html.HiddenFor(c => c.UserOID, new { id = "txtUserOID" })

                    <div class="form-group">
                        <label class="control-label" for="txtUsername">Username</label>

                        @Html.TextBoxFor(c => c.Username, new { id = "txtUsername", @class = "form-control" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtPassword">Password</label>

                        @Html.TextBoxFor(c => c.Password, new { id = "txtPassword", @class = "form-control", type = "password" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtConfirmPassword">Confirm Password</label>
                        @Html.TextBoxFor(c => c.Password, new { id = "txtConfirmPassword", @class = "form-control", type = "password" })
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="txtMobileNum">Mobile #</label>
                        @Html.TextBoxFor(c => c.MobileNum, new { id = "txtMobileNum", @class = "form-control" })
                    </div>


                    <div class="form-group">
                        <label class="control-label" for="txtAddress">Address</label>
                        @Html.TextAreaFor(c => c.Address, new { id = "txtAddress", @class = "form-control input-lg" })
                    </div>

                </div>

                <div class="col-lg-4 col-lg-offset-1">

                    <div class="form-group">
                        <label class="control-label" for="txtFirstName">First Name</label>
                        @Html.TextBoxFor(c => c.FirstName, new { id = "txtFirstName", @class = "form-control" })
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="txtLastName">Last Name</label>
                        @Html.TextBoxFor(c => c.LastName, new { id = "txtLastName", @class = "form-control" })
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="txtName">Select Role</label>
                        @Html.DropDownListFor(c => c.RoleOID, new SelectList(ViewBag.RoleList, "RoleOID", "RoleName"), new { id = "txtRoleOID", @class = "form-control" })
                    </div>


                    <div class="form-group">
                        <label class="control-label" for="txtEmail">Email ID</label>
                        @Html.TextBoxFor(c => c.EmailID, new { id = "txtEmail", @class = "form-control" })
                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtPhoneNum">Phone #</label>
                        @Html.TextBoxFor(c => c.PhoneNum, new { id = "txtPhoneNum", @class = "form-control" })
                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtName">Active</label>

                        @Html.CheckBoxFor(c => c.Active, new { id = "chkActive", @class = "form-control" })

                    </div>

                    <div class="form-group">
                        <div class="pull-right col-lg-offset-2">
                            <button id="btnSave" type="submit" class="btn btn-labeled btn-success">
                                <span class="btn-label"><i class="icon-check icon-white"></i></span>Save</button>

                            <button type="reset" class="btn btn-labeled btn-danger">
                                <span class="btn-label"><i class="icon-remove icon-white"></i></span>Cancel</button>
                        </div>
                    </div>
                </div>


            </div>
        </div>
    }


</div>

<script type="text/javascript">


    $(document).ready(function () {


        //------------------------------------------------
        $("#txtPassword").val("");
        $("#txtConfirmPassword").val("");

        if ($("#txtProductOID").val() == "") {
            $("#title").text('Add User');
        }
        else {
            $("#title").text('Edit User');
        }

        //-----------------------------------------------
        var errors = "";

        $("#btnSave").click(function () {

            errors = "";

            if ($("#txtUsername").val() == "") {
                errors = "Please enter username.";
            }

            if ($("#txtFirstName").val() == "") {
                if (errors != "")
                    errors = errors + "<br/>";

                errors = errors + "Please enter first name.";
            }

            if ($("#txtMobileNum").val() == "") {
                if (errors != "")
                    errors = errors + "<br/>";

                errors = errors + "Please enter mobile number.";
            }

            if ($("#txtAddress").val() == "") {
                if (errors != "")
                    errors = errors + "<br/>";

                errors = errors + "Please enter address.";
            }

            if ($("#txtUserOID").val() == "") {

                if ($("#txtPassword").val() == "") {
                    if (errors != "")
                        errors = errors + "<br/>";

                    errors = errors + "Please enter password.";
                }
                if ($("#txtConfirmPassword").val() == "") {
                    if (errors != "")
                        errors = errors + "<br/>";

                    errors = errors + "Please enter confirm password.";
                }
            }

            if ($("#txtPassword").val() != "") {

                if ($("#txtPassword").val() !== $("#txtConfirmPassword").val()) {
                    if (errors != "")
                        errors = errors + "<br/>";

                    errors = errors + "Password does not match.";
                }
            }

            if (errors != "") {
                create("themeroller", { title: "Error!", text: errors }, {
                    custom: true,
                    expires: false
                });
                event.preventDefault();
            }

            if ($("#txtUserOID").val() == "") {
                return checkExistingUser();
            }

            return true;

        });
        //------------------------------------------------
        function checkExistingUser() {
            var value = 0;
            $.ajax({
                type: "POST",
                async: false,
                url: '/Administrator/CheckUsername',
                data: { username: $("#txtUsername").val() },
                dataType: "json",
                processdata: true,
                success: function (result) {
                    var resultMsg = result.Message;
                    if (result.Success == true) {
                        create("themeroller", { title: 'Error!', text: result.Message }, {
                            custom: true,
                            expires: false
                        });
                        value = 1;
                    }
                },
                error: function (e) {

                }
            });

            if (value == 1)
                event.preventDefault();
        }
        //------------------------------------------------
    });// ready
</script>
